from ai.models import Projects, Models, Services
from dvadmin.utils.serializers import CustomModelSerializer
from rest_framework import serializers
from dvadmin.utils.validator import CustomUniqueValidator


class ModelsCreateUpdateSerializer(CustomModelSerializer):
    """
    模型管理 创建/更新时的列化器
    """
    name = serializers.CharField(
        max_length=50,
        validators=[
            CustomUniqueValidator(queryset=Models.objects.all(), message="模型名称必须唯一")
        ],
    )

    class Meta:
        model = Models
        fields = '__all__'


class ModelsSerializer(CustomModelSerializer):
    """
        模型管理-序列化器
    """
    # 前端获取数据显示选项对应的值
    status_name = serializers.SerializerMethodField()

    def get_status_name(self, obj):
        return obj.get_status_display()

    # 模型所属的项目类型
    # type = serializers.IntegerField(source="project.type")

    class Meta:
        model = Models
        fields = '__all__'
        read_only_fields = ["id"]


class ProjectsCreateUpdateSerializer(CustomModelSerializer):
    """
    项目管理 创建/更新时的列化器
    """

    # name = serializers.CharField(
    #     max_length=50,
    #     validators=[
    #         CustomUniqueValidator(queryset=Projects.objects.all(), message="项目名称必须唯一")
    #     ],
    # )

    class Meta:
        model = Projects
        fields = '__all__'


class ProjectsSerializer(CustomModelSerializer):
    """
    项目管理-序列化器
    """
    # 前端获取数据显示选项对应的值
    type_name = serializers.SerializerMethodField()

    def get_type_name(self, obj):
        return obj.get_type_display()

    # 项目下的模型信息
    children = ModelsSerializer(many=True)

    class Meta:
        model = Projects
        fields = '__all__'
        read_only_fields = ["id", "children"]


class ServicesCreateUpdateSerializer(CustomModelSerializer):
    """
    服务管理 创建/更新时的列化器
    """

    class Meta:
        model = Services
        fields = '__all__'


class ServicesSerializer(CustomModelSerializer):
    """
    服务管理-序列化器
    """
    # project = serializers.StringRelatedField()
    # model = serializers.StringRelatedField()
    project_name = serializers.CharField(source='project.name')
    model_name = serializers.CharField(source='model.name')

    class Meta:
        model = Services
        fields = '__all__'
        read_only_fields = ["id"]
